<?php

class AdmSideBar {

    public $root;

    public function showHtml(){
        echo
        '
         <div class="sidebar">
             
                    <div class="menu">'.
                      $this->showMenu()
                    .'
                </div>

            <div class="menu">'.$this->showtree().'

            </div> <!-- end of menu -->
            
                <center>
                <a href="http://validator.w3.org/check?uri=referer"><img style="border:0;width:88px;height:31px" src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" width="88" height="31" vspace="8" border="0" /></a> &nbsp;&nbsp;&nbsp;
                <a href="http://jigsaw.w3.org/css-validator/check/referer"><img style="border:0;width:88px;height:31px"  src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" vspace="8" border="0" /></a>
                    </center>

                <div class="cleaner"></div>
            </div> <!-- end of sidebar -->
        ';
    }

    public function showMenu(){
        if(SessionManager::isLogged()==TRUE){
                $string = '<ul>';
                $string = $string.'
                                    ';
                $string = $string.'<li><h5><span style="font-size:0.8em;">Sessão expira em: </span><font color=green size=2 face=arial><span id="clock1"></span><script>setTimeout(\'getSecs()\',1000);</script></font></h5></li>';
                $string = $string.'<li><h5><span style="font-size:0.8em;">Logado Como:</span> '.SessionManager::getBySession('nome').'</h5></li>';
                $array = array( 1 => 'Início',
                                2 => 'Cadastro de Usuário',
                                9 => 'Listagem de Usuários',
                                8 => 'Cadastro de Espécie',
                                4 => 'Editar Conta',
                                5 => 'Log Out');
                foreach ($array as $key=>$item){
                    if(($key == 2 || $key == 8 || $key==9) && SessionManager::getBySession('create')== 0) continue;
                    if($key==4){
                    $string = $string.'<li><a href="'.Config::$domainName.'/Administracao/Administracao.php?option='.$key.'&idpai='.SessionManager::getBySession('id').'">'.$item.'</a></li>';
                    continue;
                    }if($key==Config::$option){
                        $string = $string.'<li><a href="'.Config::$domainName.'/Administracao/Administracao.php?option='.$key.'" class="current">'.$item.'</a></li>';
                    }else{
                    $string = $string.'<li><a href="'.Config::$domainName.'/Administracao/Administracao.php?option='.$key.'">'.$item.'</a></li>';
                    }
                }

                $string = $string.'</ul>';
        
                return $string;
        }
    }

    public function showtree(){
        if(SessionManager::isLogged()==TRUE){
            require_once('../DAO/mySqlConnect.php'); // create the mySqlConnect class
            $mySqlConnect = new MySqlConnect();
            $current = NodeDao::db_node_by_id($mySqlConnect->getLinkDB(), Config::$current);


            $this->initLista($current);

            $mySqlConnect->closeDB();
            return $this->showArrayObject();
        }
    }


    public $matriz;
    public $object;
    public $cont=0;
    public $flag = true;

    public function  initLista($node){
        $cont = 0;
        $this->object = new node(null,null,null);
        $this->listar($node);

    }


    public function listar($node){

        $pai = $node->father;
        if($pai==null){
            $this->object->append($node);
            return;
        }else{
           if($this->flag){
                $iterator = $node->getIterator();

                $i=0;
                $array = array();

                  while($iterator->valid()) {
                   $array[$i] =  $iterator->current();
                   $i=$i+1;
                   $iterator->next();
               }
               if(count($array)>0){
                $this->object->append($array);
               }
                $this->flag=false;
                $this->object->append($node);
            }else{
                $this->object->append($node);
            }
            
            $this->cont++;
            $mySqlConnect = new MySqlConnect();
            $rec = NodeDao::db_node_by_id($mySqlConnect->getLinkDB(), $pai->id);
            if($rec!=null){
                $this->listar($rec);
            }
            
            $mySqlConnect->closeDB();
            
        }
    }

    public function getTab($int){
        return $int*10;
    }



    public function showArrayObject(){

        // include 'phpClasses/Config.php';

         $iterator = $this->object->getIterator();
         $array;
         $x=0;
         

               $string ='';

                while($iterator->valid()) {
                    $array[$x] = $iterator->current();
                    $x++;
                   $iterator->next();
                }

                $array = array_reverse($array, false);

                $i=0;

                //impressao do menu
                foreach ($array as $no){
                    if(is_array($no)){
                        $string = $string.'<ul>';
                        foreach ($no as $irmao) {
                            if($irmao->id==Config::$current){
                                $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a class="current" href ="'.Config::$domainName.'/Administracao/Administracao.php?current='.$irmao->id.'">'. $irmao->taxo->taxo.' '.$irmao->taxo->nome.'</a></li>';
                            }else{
                                $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a href ="'.Config::$domainName.'/Administracao/Administracao.php?current='.$irmao->id.'">'. $irmao->taxo->taxo.' '.$irmao->taxo->nome.'</a></li>';
                            }
                        }
                         $string = $string.'</ul>';
                    }else{
                   $string = $string.'<ul>';
                    if($no->id==Config::$current){
                         $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a class="current" href ="'.Config::$domainName.'/Administracao/Administracao.php?current='.$no->id.'">'. $no->taxo->taxo.' '.$no->taxo->nome .'</a></li>';
                    }else{
                    $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a href ="'.Config::$domainName.'/Administracao/Administracao.php?current='.$no->id.'">'. $no->taxo->taxo.' '.$no->taxo->nome .'</a></li>';
                    }
                    }
                    $i++;
                }

                for ($x=0;$x<(count($array)-1);$x++){
                     $string = $string.'</ul>';
                }
                
                return $string;
    }

  

}
?>

